/**
 *!
 * \file        b_drv_cs1231.c
 * \version     v0.0.1
 * \date        2021/04/23
 * \author      Rain
 *******************************************************************************
 * @attention
 *
 * Copyright (c) 2021 Rain
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *******************************************************************************
 */

/*Includes ----------------------------------------------*/
#include "b_drv_CS1231.h"

#include <string.h>
/**
 * \addtogroup B_DRIVER
 * \{
 */

/**
 * \addtogroup CS1231
 * \{
 */

/**
 * \defgroup CS1231_Private_TypesDefinitions
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup CS1231_Private_Defines
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup CS1231_Private_Macros
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup CS1231_Private_Variables
 * \{
 */
bCS1231_HalIf_t  bCS1231_HalIf = CS1231_DEFAULT_CONFIG;
bCS1231_Driver_t bCS1231_Driver;
/**
 * \addtogroup CS1231_Exported_Functions
 * \{

*/
#if __GNUC__
void __attribute__((weak)) bCS1231Callback(int value)
#else
__weak void bCS1231Callback(int value)
#endif
{

}

/**
 * \}
 */

/**
 * \defgroup CS1231_Private_FunctionPrototypes
 * \{
 */

void _bCS1231_Delay(uint32_t i)
{
    for (; i > 0; i--)
    {
        //__nop();
    }
}

static void _bCS1231Polling(void)
{
    bCS1231_HalIf_t *config = bCS1231_Driver._hal_if;
    if (bHalGPIO_ReadPin(config->data.port, config->data.pin) == 0)
    {
        uint8_t i;
        int     nData = 0;
        for (i = 0; i < 25; i++)
        {
            bHalGPIO_WritePin(config->clck.port, config->clck.pin, 1);
            _bCS1231_Delay(10);
            bHalGPIO_WritePin(config->clck.port, config->clck.pin, 0);
            _bCS1231_Delay(10);
            nData |= bHalGPIO_ReadPin(config->data.port, config->data.pin);
            nData <<= 1;
        }
        nData >>= 1;

        if (bHalGPIO_ReadPin(config->data.port, config->data.pin) == 0)
        {
            //b_log("CS1231 ERROR %x\r\n", nData);
            return;
        }
        else
        {
            int v = (nData << 8) / 256;
            bCS1231Callback(v);
            //b_log("bCS1231Callback  %d\r\n", v);
        }
    }
}
/**
 * \}
 */

/**
 * \}
 */

int bCS1231_Init()
{
    bCS1231_Driver.status   = 0;
    bCS1231_Driver.read     = NULL;
    bCS1231_Driver.write    = NULL;
    bCS1231_Driver.open     = NULL;
    bCS1231_Driver.close    = NULL;
    bCS1231_Driver.ctl      = NULL;
    bCS1231_Driver._hal_if  = &bCS1231_HalIf;
    bCS1231_HalIf_t *config = bCS1231_Driver._hal_if;
    bHalGPIO_Config(bCS1231_HalIf.clck.port, bCS1231_HalIf.clck.pin, B_HAL_GPIO_OUTPUT,
                    B_HAL_GPIO_NOPULL);
    bHalGPIO_Config(bCS1231_HalIf.data.port, bCS1231_HalIf.data.pin, B_HAL_GPIO_INPUT,
                    B_HAL_GPIO_PULLUP);
    b_log("bCS1231_Init \r\n");
    return 0;
}

BOS_REG_POLLING_FUNC(_bCS1231Polling);
bDRIVER_REG_INIT(bCS1231_Init);

/**
 * \}
 */

/**
 * \}
 */

/**
 * \}
 */

/************************ Copyright (c) 2020 Bean *****END OF FILE****/
